﻿using BluetoothMachine.Domains;
using BluetoothMachine.ViewModels;
using BluetoothMachine.ViewModels.DialogHostsModel;
using BluetoothMachine.Views;
using BluetoothMachine.Views.DialogHosts;
using MaterialDesignThemes.Wpf;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace BluetoothMachine
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        #region WIN32API
        [DllImport("user32.dll")]
        static extern int SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
        const uint SWP_NOMOVE = 0x0002;
        const uint SWP_NOSIZE = 0x0001;
        static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
        #endregion

        public MainWindow()
        {
            InitializeComponent();
            GloblaStart.Synchronization = SynchronizationContext.Current;//获取当前线程的上下文，协调跨线程的同步操作
        }

        private void DialogHost_Loaded(object sender, RoutedEventArgs e)
        {
            _ = ServiceDiscovery.GetObject;
            _ = TaskManagers.GetObject;
            DataContext = new MainWindowViewModel();
            WindowState = WindowState.Maximized;
            //IntPtr intPtr = new WindowInteropHelper(this).Handle;
            //_ = SetWindowPos(intPtr, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
            new MainTwo().Show();
        }

        private void ButExit_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (!GlobalStatics.FixedParameter.IsDeBug) ExpandHelper.LTDMC.dmc_board_close();  // 关闭运动控制卡
            Application.Current.Shutdown();
        }

        private void Mini_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            WindowState = WindowState.Minimized;
        }
    }
}
